package com.hongxun.design.adapters;

import android.content.Context;
import android.net.Uri;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.bean.PhotoBean;
import com.hongxun.design.bean.PhotoGraphBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.ui.mygridview.GridViewInListView;

import java.util.List;


/**
 * Created by zerdoor_pc on 2016/4/27.
 */
public class PhotoGraphAdapter extends BaseAdapter {
    private List<PhotoGraphBean> listData;
    private Context context;
    LayoutInflater inflater;

    public PhotoGraphAdapter(List<PhotoGraphBean> listData, Context context) {
        this.listData = listData;
        this.context = context;
        inflater = LayoutInflater.from(context);
        MyLog.L(MyApp.AppName + " listData=" + JSON.toJSONString(listData));
    }

    /**
     * 添加方法*
     */
    public void Refresh(int CODE, String path) {
        PhotoGraphBean bean = null;
        switch (CODE) {
            case Constant.TypePosition:
                bean = listData.get(0);

                break;
            case Constant.TypePassageway:
                bean = listData.get(1);

                break;
            case Constant.TypeEnvironmental:
                bean = listData.get(2);

                break;
        }
        if (bean == null) return;
        if (bean.getPhotos().size() == 1 && bean.getPhotos().get(0).getUrl().length() == 0) {
            bean.getPhotos().remove(0);
        }
        bean.getPhotos().add(0, new PhotoBean(CODE, null, path));
        notifyDataSetChanged();
    }

    /**
     * 添加方法*
     */
    public void Refresh(int CODE, String path_thumb, String path_url, String photo_id, String uuid,
                        String flag, String exploration_id, String Create_time) {
        PhotoGraphBean bean = null;
        switch (CODE) {
            case Constant.TypePosition:
                bean = listData.get(0);
                break;
            case Constant.TypePassageway:
                bean = listData.get(1);
                break;
            case Constant.TypeEnvironmental:
                bean = listData.get(2);
                break;
        }
        if (bean == null) return;
        if (bean.getPhotos().size() == 1 && bean.getPhotos().get(0).getUrl().length() == 0) {
            bean.getPhotos().remove(0);
        }

        bean.getPhotos().add(0, new PhotoBean(CODE, null, path_thumb, path_url, photo_id, uuid, flag,
                exploration_id, Create_time));
        notifyDataSetChanged();
    }

    /**
     * 删除方法*
     */
    public void Delect(int CODE, String path) {
        List<PhotoBean> bean = null;
        PhotoGraphBean photoGraphBean = null;
        switch (CODE) {
            case Constant.TypePosition:
                photoGraphBean = listData.get(0);
                bean = photoGraphBean.getPhotos();
                break;
            case Constant.TypePassageway:
                photoGraphBean = listData.get(1);
                bean = photoGraphBean.getPhotos();
                break;
            case Constant.TypeEnvironmental:
                photoGraphBean = listData.get(2);
                bean = photoGraphBean.getPhotos();
                break;
        }
        if (photoGraphBean == null || bean == null) return;

        for (PhotoBean photo : bean) {
            if (photo.getUrl().equals(path)) {
                bean.remove(photo);
                break;
            }
        }
        if (bean.size() == 0) {
            bean.add(new PhotoBean(CODE, null, ""));
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int i) {
        return listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        synchronized (inflater) {
            Holder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.listview_item_photograph, null);
                holder = new Holder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            PhotoGraphBean bean = null;
            try {
                bean = listData.get(position);
                holder.photograph_type.setImageURI(Uri.parse(getImageByType(bean.getType())));
                holder.photograph_title.setText(bean.getTitle());
                holder.photograph_position.setText(bean.getPosition());
//                holder.photograph_number.setText(MString.PhotoNum);
//                holder.photograph_number.append(bean.getNumber());
                List<PhotoBean> photos = bean.getPhotos();
//                SpannableString sp = new SpannableString(bean.getLackNumber());
//                sp.setSpan(new ForegroundColorSpan(R.color.project_blue), 0, bean.getLackNumber().length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//                holder.photograph_number.append(sp);
//                holder.photograph_number.setVisibility(View.GONE);
                if (!MyApp.ReadOnly) {
                    holder.photograph_button.setOnClickListener(bean.getListener());
                } else {
                    holder.photograph_button.setBackgroundResource(R.drawable.gray_item_6);
                    holder.photograph_button.setTextColor(context.getResources().getColor(R.color.white));
                }
                switch (bean.getType()) {
                    case Constant.TypePosition:
                        holder.photograph_right_button.setVisibility(View.GONE);
                        break;
                    case Constant.TypePassageway:
                        holder.photograph_right_button.setVisibility(View.VISIBLE);
                        SpannableString styledText1 = new SpannableString(IconSourse.RECORDCROSSING);
                        styledText1.setSpan(new TextAppearanceSpan(context, R.style.style0), 0, styledText1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        SetTypeface.set(holder.photograph_right_button, styledText1);
                        holder.photograph_right_button.append(" 记录交叉跨越");
                        holder.photograph_right_button.setOnClickListener(bean.getListener_right());
                        break;
                    case Constant.TypeEnvironmental:
                        holder.photograph_right_button.setVisibility(View.VISIBLE);
                        SetTypeface.set(holder.photograph_right_button, IconSourse.EDIT);
                        holder.photograph_right_button.append(" 编辑环境描述");
                        holder.photograph_right_button.setOnClickListener(bean.getListener_right());
                        break;
                }
                /**图片适配**/
                PhotoGraphGridAdapter adapter = new PhotoGraphGridAdapter(photos, context, bean.getType());
                holder.photograph_gridView.setAdapter(adapter);

            } catch (Exception e) {
                MyLog.L(MyApp.AppName + "适配错误：" + e.toString());
            }
            return convertView;
        }
    }

    private String getImageByType(int type) {
        String url = MString.LOCALIMAGEHEAD + R.mipmap.zhanweidianshe;
        switch (type) {
            case Constant.TypePosition:
                url = MString.LOCALIMAGEHEAD + R.mipmap.one;
                break;
            case Constant.TypePassageway:
                url = MString.LOCALIMAGEHEAD + R.mipmap.two;
                break;
            case Constant.TypeEnvironmental:
                url = MString.LOCALIMAGEHEAD + R.mipmap.three;
                break;
        }
        return url;
    }

    public class Holder {
        public SimpleDraweeView photograph_type;
        public GridViewInListView photograph_gridView;
        public TextView photograph_title, photograph_position, photograph_number, photograph_button, photograph_right_button;

        Holder(View convertView) {
            photograph_type = (SimpleDraweeView) convertView.findViewById(R.id.photograph_type);
            photograph_title = (TextView) convertView.findViewById(R.id.photograph_title);
            photograph_position = (TextView) convertView.findViewById(R.id.photograph_position);
            photograph_number = (TextView) convertView.findViewById(R.id.photograph_number);
            photograph_button = (TextView) convertView.findViewById(R.id.photograph_button);
            photograph_right_button = (TextView) convertView.findViewById(R.id.photograph_right_button);
            photograph_gridView = (GridViewInListView) convertView.findViewById(R.id.photograph_gridView);
            SetTypeface.set(photograph_button, IconSourse.PHOTOGRAPH);
            photograph_button.append(" 拍摄照片");
        }
    }


    abstract class MyLongClick implements View.OnLongClickListener {
        PhotoGraphGridAdapter adapter;

        public MyLongClick(PhotoGraphGridAdapter adapter) {
            this.adapter = adapter;
        }

        @Override
        public boolean onLongClick(View view) {
            return MyonLongClick(adapter, view);
        }

        public abstract boolean MyonLongClick(PhotoGraphGridAdapter adapter, View view);
    }
}
